package nccu.webapp.controller;

import nccu.model.AccountCode;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;


/**
 *
 * <p><a href="CreateExcelDocController.java"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:chris.oberle@gmail.com">Chris Oberle</a>
 */
@Controller
@RequestMapping("/excel")

public class CreateExcelDocController implements ServletContextAware {

    protected final transient Log log = LogFactory.getLog(getClass());
    private ServletContext servletContext;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView generate(HttpServletRequest request)
    throws Exception {
        File file = new File("C:\\dev\\credit-union\\src\\main\\resources\\NCCUD_Exam_Template.xlsm");
        File toFile = new File("C:\\tmp\\2012\\1Q2012_CoastalFederal_081212.xlsm");
        FileUtils.copyFile(file, toFile);
//        InputStream inp = new FileInputStream(file);
//        Workbook wb = WorkbookFactory.create(inp);
//        Sheet generalLedger = wb.getSheetAt(1);
//        Row row = generalLedger.getRow(4);
//        row.getCell(2).setCellValue(100);
//        FileOutputStream fileOut = new FileOutputStream(file);
//        wb.write(fileOut);
//        fileOut.close();
        return new ModelAndView("redirect:/mainMenu");
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    protected ServletContext getServletContext() {
        return servletContext;
    }
}
